package xuan.mall.system.interfaces.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import xuan.mall.common.enums.ResultCode;

@JsonInclude(JsonInclude.Include.NON_NULL)  // 忽略null字段
@Data
public class Response<T> {
    private int code;
    private String message;
    private T data;
    private Long timestamp = System.currentTimeMillis();  // 时间戳

    // 成功响应工厂方法
    public static <T> Response<T> success(T data) {
        return new Response<>(ResultCode.SUCCESS, data);
    }
    
    // 错误响应工厂方法
    public static <T> Response<T> error(ResultCode code, String customMessage) {
        return new Response<>(code.getCode(), 
                             customMessage != null ? customMessage : code.getMessage(), 
                             null);
    }

    // 构造方法（protected）
    protected Response(ResultCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }
    
    protected Response(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}